#include "ColouredVertexBuffer.h"

namespace ds {

ColouredVertexBuffer::ColouredVertexBuffer(D3DPRIMITIVETYPE primitiveType) : VertexBuffer(primitiveType) {
	m_FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
	m_vertexSize = sizeof(ColouredVertex);
}

ColouredVertexBuffer::~ColouredVertexBuffer() {
	//SAFE_RELEASE(vertexBuffer);
	Release();
}

void ColouredVertexBuffer::addVertex(float x,float y,float z) {
    ColouredVertex tv;
    tv.x = x;
    tv.y = y;
    tv.z = z;    
	tv.color = Color(1.0f,1.0f,1.0f,1.0f);
    m_Vertices.push_back(tv);
}

void ColouredVertexBuffer::clear() {
	m_Vertices.clear();
}

void ColouredVertexBuffer::addVertex(float x,float y,float z,Color color) {
    ColouredVertex tv;
    tv.x = x;
    tv.y = y;
    tv.z = z;
	tv.color = color;
    m_Vertices.push_back(tv);
}

bool ColouredVertexBuffer::createBuffer() {
	IDirect3DDevice9 * pDevice = gEngine->getDevice();
	m_numVertices = getSize();
    pDevice->CreateVertexBuffer( m_numVertices * sizeof( ColouredVertex),0,m_FVF ,D3DPOOL_DEFAULT, &vertexBuffer, NULL );
    ColouredVertex *pVertices;
	vertexBuffer->Lock( 0, sizeof( ColouredVertex ), ( void** )&pVertices, 0 );	
	for ( UINT i = 0; i < m_numVertices; ++i) {		
		pVertices->x = m_Vertices[i].x;
		pVertices->y = m_Vertices[i].y;
		pVertices->z = m_Vertices[i].z;		
		pVertices->color = m_Vertices[i].color;		
		pVertices++;
	}
    vertexBuffer->Unlock();		
    return true;
}

};
